<template>
    <div class="dialog__wrap" v-show="state">
        <transition name="background-fade" @after-leave="afterLeave">
            <div class="dialog__background" v-if="show"></div>
        </transition>
        <transition name="fade-move">
            <div class="dialog" v-if="show">   
                <i class="iconfont icon-cuo dialog__head__btn" @click="close"></i>
                <div  class="dialog__head">
                    <!-- {{dialog_config.dialog_title}} -->
                    111
                    <p>{{dialog_config.dialog_desc}}</p>
                </div>
                <div class="dialog__body">
                    <span :class="validClass($v.textFrom.mobile)">
                        <input type="text" v-model="textFrom.mobile" @input="$v.textFrom.mobile.$touch()" :placeholder="dialog_config.phonetips">
                    </span>
                    <span :class="validClass($v.textFrom.captcha)">
                        <input type="text" maxlength="4" v-model="textFrom.captcha" @input="$v.textFrom.captcha.$touch()" :placeholder="dialog_config.smstips" style="width: 56%;">
                    </span>
                    <span id="btn" @click="sendSms" :disabled="contpending">{{contpending ? '重新发送 (' + count + '秒)' : '获取验证码'}}</span>
                </div>
                <p v-if="$v.textFrom.captcha.$dirty && $v.textFrom.captcha.$invalid">{{coder}}</p>
                <div class="dialog__footer">
                    <Btn @click.native="confirm" class="dialog__footer__btn dialog__footer__btn--success" v-if="dialog_config.okText">{{dialog_config.okText}}</Btn>
                    <Btn @click.native="cancel" class="dialog__footer__btn dialog__footer__btn--fail" v-if="dialog_config.cancelText">{{dialog_config.cancelText}}</Btn>
                </div>
            </div>
        </transition>
    </div>
</template>

<script>
import Btn from "./Button";
import { required, minLength } from "vuelidate/lib/validators";
import { telNumPattern } from "@/class/product-validation.class";


class mobileDialog {
  dialog_title;
  dialog_desc;
  phonetips;
  smstips;
  okText;
  cancelText;
}

export default {
  name: "DialogModal",
  data: function() {
    return {
      dialog_config: new mobileDialog(),
      state: false,
      show: false,
      contpending: false,
      count: 5,
      pending: false,
      textFrom: {
          mobile: '',
          captcha: '',
      },
      verificationStatus:'',
      coder: '验证码错误呢 亲~ 请重新获取验证码呢',
      identCode: '',   //id btn变量
      identColor: '',   //id btn color的变量
    };
  },
  validations: {
      textFrom: {
          mobile: {
              required,
          },
          captcha: {
              required,
              minLength: minLength(4)
          },
      }
  },
  methods: {
    //  verificationSMS: function (mobile,captcha) {
    //     let that = this;
    //     this.$fetch(that.$path + '/api.php/api/v2/verificationSMS/'+captcha+'/'+mobile)
    //       .then((res) => {
    //         if(res.code == 200){ 
    //             that.verificationStatus = 1;
    //         }else{ 
    //            that.verificationStatus = 0;
              
    //         }
    //       });
    // },
    // 根据字段验证转换 class 状态
    validClass(data) {
        if (!data) return {};
        return {
            error: data["$dirty"] && data["$invalid"],
            success: data["$dirty"] && !data["$invalid"]
        };
    },
    close() {
      this.show = false;
      this.$emit('done');
    },
    afterLeave() {
      this.state = false;
      this.$emit("update:control", false);
    },
    confirm() {
      // this.close();
      // this.verificationSMS(this.textFrom.mobile,this.textFrom.captcha)
      // let that = this;
      //   this.$fetch(that.$path + '/api.php/api/v2/verificationSMS/'+this.textFrom.captcha+'/'+this.textFrom.mobile)
      //     .then((res) => {
      //       if(res.code == 200){ 
      //         // this.$emit("done",1);
      //         console.log('手机验证');
      //       }else{ 
      //         //  this.$emit("done",0);
      //       }
      // });
      this.$emit("done");
    },
    cancel() {
      this.close();
      this.$emit("done", false);
    },
    sendSms() {
      let that = this;
      if (that.textFrom.mobile == "") {
          that.$openTips({
              text: "手机号码不能为空！",
              time: 1000
          });
          return;
      }
      if (!/^1[3456789]\d{9}$/.test(that.textFrom.mobile)) {
          that.$openTips({
              text: "电话号码格式错误",
              time:1000
          });
          return;
      }
      that.$nextTick(() => {
          that.contpending = true;
          if (that.pending) return;
          that.pending = true;
          let timer = null,
              count = 5;
          clearInterval(timer);
          timer = setInterval(_ => {
              if (count <= 0) {
                  that.contpending = false;
                  that.pending = false;
                  clearInterval(timer);
                  that.count = count = 5;
                  that.identCode = document.getElementById('btn').style.borderColor;
                  that.identCode="#e50838";
                  that.identColor = document.getElementById('btn').style.color;
                  that.identColor="#e50838";
              } else {
                  --count;
                  that.identCode="#d8d8d8";
                  that.identColor="#999";
                  that.count = count;
              }
          }, 1000);
      })
      that.$fetch(that.$path+'/index.php/api/v2/SMS/'+ that.textFrom.mobile).then((res) => {
         if(res.code == 200) {
           

         }               
      })
    },
  },
  watch: {
    options(n, o) {
      if (n) {
        this.dialog_config = n;
      }
    },
    control(n, o) {
      this.show = n;
      if (n) {
        this.state = n;
      }
    }
  },
  components: {
    Btn
  },
  props: ["options", "control", "done"]
};
</script>

<style lang="scss" scoped>
.dialog {
  width: 5.48rem;
  padding-bottom: 0.2rem;
  border-radius: 4px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
  background-color: #fff;
  &__wrap {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10000;
  }
  &__background {
    height: inherit;
    width: inherit;
    background-color: rgba(0, 0, 0, 0.5);
  }
  &__head {
    width: 5rem;
    line-height: 1;
    text-align: center;
    position: relative;
    margin: 0.3rem auto 0.3rem;
    font-size: 0.28rem;
    color: #333;
    p {
      font-size: 0.22rem;
      color: #999;
      line-height: 0.48rem;
    }
    &__btn {
      position: absolute;
      right: 0;
      top: 0;
      font-size: 0.16rem;
      color: #333;
      width: 0.48rem;
      height: 0.48rem;
      border-radius: 50%;
      line-height: 0.48rem;
    }
  }
  &__body {
    padding: 0 0.2rem;
    font-size: 0.22rem;
    font-family: "\5FAE\8F6F\96C5\9ED1";
    color: #999;
    white-space: normal;
    word-break: break-all;
    text-align: center;
    line-height: 0.36rem;
    text-align: left;
    input {
      border: 1px solid #ccc;
      // height: 0.6rem;
      line-height: 0.6rem;
      box-sizing: border-box;
      border-radius: 4px;
      -moz-box-shadow: none;
      -webkit-box-shadow: none;
      box-shadow: none;    
      width: 100%;
      font-size: 0.24rem;
      color: #333;
      margin-bottom: 0.2rem;
      padding: 0 0.2rem;
    }
    #btn {
      border: 1px solid #e50838;
      font-size: 0.22rem;
      color: #e50838;
      border-radius: 4px;
      width: 42%;
      height: 0.6rem;
      line-height: 0.58rem;
      display: inline-block;
      vertical-align: top;
      padding: 0 0.12rem;
      text-align: center;
    }
  }
  &__footer {
    font-size: 0;
    text-align: center;
    margin-top: 0.2rem;
    &__btn {
      font-size: 0.28rem;
      line-height: 1;
      margin-bottom: 0.2rem;

      &:active {
        background-color: #f1f1f1;
        opacity: 1;
      }

      &--success {
        width: 4.6rem;
        height: 0.8rem;
        font-size: 0.28rem;
        color: #fff;
        background-color: #e50838;
        border-radius: 4px;
        vertical-align: super;

        &:active {
          background-color: #e50838;
          opacity: 1;
          color: #fff;
        }
      }
      &--fail {
        background-color: #fff;
        color: #e50838;
        width: 4.6rem;
        height: 0.8rem;
        border-radius: 4px;
        border: 1px solid #e50838 !important;
        &:active {
          background-color: #fff;
          opacity: 1;
          color: #e50838;
        }
      }
    }
  }
}
.background-fade {
  &-enter {
    opacity: 0;
  }
  &-enter-active {
    transition: opacity 0.2s;
  }
  &-enter-top {
    opacity: 1;
  }
  &-leave {
    opacity: 1;
  }
  &-leave-active {
    transition: opacity 0.2s;
    opacity: 0;
  }
}
.fade-move {
  &-enter {
    opacity: 0.6;
    top: 45%;
  }
  &-enter-active {
    transition: top 0.4s ease-in-out, opacity 0.4s ease-in-out;
  }
  &-enter-to {
    opacity: 1;
    top: 50%;
  }
  &-leave {
  }
  &-leave-active {
  }
  &-leave-to {
  }
}
</style>